#include "printSyslog.h"

// 假设有一个结构体存储客户端连接信息

// 记录客户端连接信息

void log_client_connect(ClientInfo *client)
{
    char timestamp[32];
    strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", localtime(&client->connect_time));

    syslog(LOG_INFO, "[CLIENT CONNECT] IP: %s, port: %hu, TIME: %s", client->ip, client->port, timestamp);
}

// 记录客户端操作记录
void log_client_operation(const char *cmd, time_t operation_time)
{
    char op_timestamp[32];
    char *cmdName[8] = {"cd", "ls", "pwd", "puts", "gets", "rm", "mkdir", "rmdir"};
    strftime(op_timestamp, sizeof(op_timestamp), "%Y-%m-%d %H:%M:%S", localtime(&operation_time));

    syslog(LOG_INFO, "[CLIENT OPERATION] Operation: %s, TIME: %s", cmd, op_timestamp);
}

int printServerLogs(char *ip_str, uint16_t client_port, char *cmd)
{

    // 初始化syslog
    openlog(NULL, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_USER);
    ClientInfo client;
    if (ip_str != NULL)
    {
        // 假设获取到客户端连接信息并记录
        
        client.connect_time = time(NULL);
        memcpy(&client.ip, ip_str, strlen(ip_str));
        client.port=client_port;
        log_client_connect(&client);
    }
    else
    {
        // 假设某个时间点发生了客户端操作，并记录
        time_t operation_time = time(NULL); // 获取当前时间作为操作时间
        log_client_operation(cmd, operation_time);
    }

    closelog(); // 关闭syslog

    return 0;
}
